﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using AppFramework.DinosaursParkMes.DictionariesModel.IService;
using AppFramework.DinosaursParkMes.Execute.Dto;
using AppFramework.DinosaursParkMes.Material;
using AppFramework.DinosaursParkMes.ProductWork.Dto;
using AppFramework.DinosaursParkMes.UniqueIdCode.Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NPOI.POIFS.Crypt.Dsig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppFramework.DinosaursParkMes.UniqueIdCode
{
    public class UniqueIdCodeAppService : AppFrameworkAppServiceBase, IUniqueIdCodeAppService
    {
        private readonly IRepository<MaterialModel.UniqueIdCode> repository;
        private readonly IDictionariesService dictionariesService;

        public UniqueIdCodeAppService(IRepository<MaterialModel.UniqueIdCode> repository, IDictionariesService dictionariesService)
        {
            this.repository = repository;
            this.dictionariesService = dictionariesService;
        }
        /// <summary>
        /// 条码格式下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ListResultDto<BarcodeStandardlist>> BarcodeStandard()
        {
            var list= await dictionariesService.GetDictionary("条码格式");
            var data=new List<BarcodeStandardlist>();
            foreach (var item in list)
            {
              data.Add(new BarcodeStandardlist () { Name=item.Text , Value=item.Value });
            }
            return new ListResultDto<BarcodeStandardlist>
            {
                Items = data.ToList(),
            };
        }

        /// <summary>
        /// 条码类型下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Barcodetypelist>> GetBarcodetype()
        {
            var data = from x in await dictionariesService.GetDictionary("条码类型")
                       select new Barcodetypelist
                       {
                           Text = x.Text,
                       };
            return data.ToList();
        }

        /// <summary>
        /// 赋码列表
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ListResultDto<ShowUniqueIdCodeDto>> GetUniqueIdCode()
        {
            var list = repository.GetAll();
            var list2 =  ObjectMapper.Map<List<ShowUniqueIdCodeDto>>(list);
            list2.ForEach(x =>
            {
                switch (x.State)
                {
                    case "True": x.State = "生效";  break;
                    case "False": x.State = "失效";  break;
                }
            });
            var data=from x in list2
                     select x;
            var count = list2.Count();
            ////条码类型
            //if (!string.IsNullOrEmpty(dto.Barcodetype)) {
            //    data = data.Where(x => x.Barcodetype.Equals(dto.Barcodetype));   }
            ////状态
            //if (!string.IsNullOrEmpty(dto.State)) {
            //    data = data.Where(x => x.State.Contains(dto.State));   }
            return new ListResultDto<ShowUniqueIdCodeDto>
            {
                Items = data.ToList()
            };
        }
        /// <summary>
        /// 新增编码
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet,Route("UniqueIdCodeAdd")]
        public async  Task<UniqueIdCodeAddDTO> UniqueIdCodeAdd(UniqueIdCodeAddDTO dto)
        {
            var data = ObjectMapper.Map<MaterialModel.UniqueIdCode>(dto);
            data.Barcode = "111";
              await   repository.InsertAsync(data);
            return dto;
        }

    }
}
